package com.example.android_8_26

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.animateBounds
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.example.android_8_26.entity.Goods
import com.example.android_8_26.ui.theme.Android_8_26Theme
import com.example.android_8_26.utils.ApiServer
import com.example.android_8_26.utils.retrofit
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            Android_8_26Theme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                        name = "Android",
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

@SuppressLint("CoroutineCreationDuringComposition", "UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    val scope = rememberCoroutineScope()
    val server = retrofit.create(ApiServer::class.java)
    var goods: MutableList<Goods> = remember { mutableStateListOf() }
    var sanckbar: SnackbarHostState = remember { SnackbarHostState() }
    scope.launch {
        val res = server.getGoods(0,1,100)
        if (res.code == 200) {
            goods.addAll(res.data)
            res.data.forEach {
                Log.d("TAG","Greeting:$it")
            }
        }
    }
    Scaffold(
        snackbarHost = { SnackbarHost(sanckbar) }
    ) {
        // 垂直网格网格
        LazyVerticalGrid(columns = GridCells.Fixed(3)) {
            items(goods) {
                Card(
                    modifier = Modifier.padding(5.dp),
                    onClick = {
                        Log.d("TAG","Greeting:${it.goodsDesc}")
                    }
                ) {
                    Column {
                        AsyncImage(
                            model = it.goodsIcon,
                            contentDescription = null,
                            contentScale = ContentScale.FillWidth,
                            modifier = Modifier
                                .fillMaxWidth()
                                .aspectRatio(1f)
                        )
                        Text(
                            text = it.goodsDesc,
                            maxLines = 2,
                            overflow = TextOverflow.Ellipsis
                        )
                    }
                }
            }
        }
    }

//    Column(modifier = modifier) {
//        Text(
//            text = "你好呀！"
//        )
//        Text(text = "第二行文本")
//        // 图片显示， android中 image只能显示本地图片，网络图片需要自己去加载（get）
//        androidx.compose.foundation.Image(
//            painter = painterResource(id = R.drawable.ic_launcher_background),
//            contentDescription = ""
//        )
//        // 加载网络图片 -- 通过第三方依赖
//        AsyncImage(
//            model = "https://s1.aigei.com/src/img/jpeg/30/30c5b48f3e3d4b98bdcd3cce92b45f94.jpeg?imageMogr2/auto-orient/thumbnail/!240x320r/gravity/Center/crop/240x320/quality/85/%7CimageView2/2/w/240&e=2051020800&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:S4id1_EuWQmkIx10hRaYOQU1zVI=",
//            contentDescription = null
//        )
//    }
}

@Preview(showBackground = true, widthDp = 720, heightDp = 1440) // 预览
@Composable
fun GreetingPreview() {
    Android_8_26Theme {
        Greeting("Android")
    }
}